import Vue from "vue";
import App from "./App.vue";
import router, { initDynamicRoutes } from "./router";
import store from "./store";
import "@/plugins/ui.js";
// import '@/plugins/svgIcon.js'
import MainDirective from "./util/directives";
import GlobalComponents from "./components";

Vue.prototype.$path = process.env.VUE_APP_HTTP + "/storage/upload/";

Vue.config.productionTip = false;
Vue.use(MainDirective);

Vue.prototype.$baseUrl = process.env.VUE_APP_HTTP;

// 注册全局组件
GlobalComponents(Vue);

initDynamicRoutes();

router.beforeEach((to, from, next) => {
  // 登录拦截
  if (!to.meta.noRequireAuth) {
    // 判断跳转的路由是否需要登录
    if (sessionStorage.getItem("token")) {
      // 判断token是否存在
      next(); // 已登录
    } else {
      // 未登录
      next({
        path: "/login",
        query: {
          source: to.fullPath,
        },
      });
    }
  } else {
    // 无需登录直接放行
    next();
  }
});

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");
